package com.china.zhuoyue.ui.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.china.zhuoyue.R
import com.china.zhuoyue.databinding.ViewProtocolBinding
import com.china.zhuoyue.ui.activity.commom.openUrl
import com.china.zhuoyue.ui.ext.bindViewClick

/**
 *所有协议
 */
class ProtocolView(context: Context, attrs: AttributeSet?) :
    ConstraintLayout(context, attrs), View.OnClickListener {
    companion object {
        const val LOGIN: Int = 1 // 登录协议
        const val REGISTER: Int = 2 // 注册
    }

    private var protocol: Int
    private val vb: ViewProtocolBinding by lazy {
        ViewProtocolBinding.inflate(LayoutInflater.from(context), this, true)
    }

    init {
        bindViewClick(this, vb.textProtocol2, vb.textProtocol4)
        val attributes = context.obtainStyledAttributes(attrs, R.styleable.ProtocolView)
        protocol = attributes.getInt(R.styleable.ProtocolView_ad_protocol, LOGIN)
        attributes.recycle()
//        when (protocol) {
//            REGISTER -> {
//                vb.textProtocol.text = R.string.text_login_protocol_register.getString()
//            }
//        }
    }

    override fun onClick(v: View?) {
        when (protocol) {
            LOGIN -> {
                when (v) {
                    vb.textProtocol2 -> {
                        context.openUrl("")
                    }
                    vb.textProtocol4 -> {
                        context.openUrl("")
                    }
                }
            }
            else -> {
            }
        }
    }

    fun isChecked() = vb.cbProtocol.isChecked

    fun setChecked(isChecked: Int) = run { vb.cbProtocol.isChecked = isChecked == 1 }

    fun getNotCheckToast() = "请先同意协议"
}